/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.service;

import ac.pbp.core.exception.ServiceException;
import ac.pbp.core.filter.FilterCriteria;
import ac.pbp.core.paging.FilterAndPaging;
import ac.pbp.dao.GenreDao;
import ac.pbp.dto.GenreDto;
import ac.pbp.dto.filter.GenreFilterCriteria;
import ac.pbp.model.Genre;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Kim
 */
@Component
@Transactional
public class GenreService {
    
    @Autowired
    private GenreDao genreDao;
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public void addGenre(Genre genre) throws ServiceException {
                
        if (genre.getKodeGenre() != null) {
        
            genreDao.add(genre);
        } 
    }
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public String updateGenre(Genre genre) throws ServiceException {
        
        if (genre.getKodeGenre() != null) {
            
            Genre genreInner = genreDao.load(genre.getKodeGenre());
            genreInner.setKodeGenre(genre.getKodeGenre());
            genreInner.setNamaGenre(genre.getNamaGenre());
            
            genreDao.update(genreInner);  
        }
        
        return genre.getKodeGenre();
    }
    
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public Genre loadGenre(String kodeGenre) {
        return genreDao.load(kodeGenre);
    }
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public void deleteGenre(String idArray) {
        String[] idArrayRay = idArray.split(",");
        for (int i = 0; i < idArrayRay.length; i++) {
            try {
                if (!idArrayRay[i].equals("")) {
                    genreDao.delete(genreDao.load(idArrayRay[i]));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    public GenreDto filterGenre(FilterCriteria filterCriteria) {
        GenreDto genreDto = new GenreDto();
        
        genreDto.setGenreList(FilterAndPaging.filter(this.genreDao, filterCriteria));
        genreDto.setFilterCriteria((GenreFilterCriteria) filterCriteria);

        return genreDto;
    }
    
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public List<Genre> loadAll() {
        return genreDao.loadAll();
    }
}
